﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace AMS.DD35.DbAccess.SqlClient
{
    public class SqlEDMClassConnector
    {
        public SqlEDMClassConnector()
        { }

        public IEnumerable<Class> GetClasses()
        {
            using (DD35DataEntities dbContext = new DD35DataEntities())
            {
                return dbContext.Classes.Where(u => u.Class_ID >= 0).AsEnumerable();
            }
        }

        public void SaveClass(Class c)
        {
            // insert new or update existing?
            using (DD35DataEntities dbContext = new DD35DataEntities())
            {
                Class c2 = dbContext.Classes.FirstOrDefault(p => p.Class_ID == c.Class_ID);
                if (c2 == null)
                {
                    dbContext.AddToClasses(c);
                    dbContext.SaveChanges();
                }
                else
                {
                    // how to save changes...?
                    dbContext.Classes.Detach(c2);
                    dbContext.Classes.Attach(c);
                    dbContext.ObjectStateManager.ChangeObjectState(c, EntityState.Modified);
                    dbContext.SaveChanges();
                }
            }
        }
    }
}
